package com.cheyun.netsalev3.view.cluefollowup

import android.content.Intent
import androidx.annotation.NonNull
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.viewpager.widget.ViewPager
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.HomePageAdapter
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.bean.order.OrderChooseData
import com.cheyun.netsalev3.databinding.ActivityClueFollowUpBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.ClueFollowUpActivityViewModel

class ClueFollowUpActivity : BaseActivity<ActivityClueFollowUpBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_clue_follow_up
    }

    override fun initViewModel(): ViewModel? {
        var leve = intent.getStringExtra("leve")
        var temdata = intent.getSerializableExtra("data")

        var come = ""
        if(intent.hasExtra("come"))
            come = intent.getStringExtra("come")
        var isNew = false
        if(intent.hasExtra("isNew"))
            isNew = intent.getBooleanExtra("isNew",false)
        var data = ClueDetails()
        temdata?.apply {
            data = this as ClueDetails
        }
        var waitAddData = EditClueParam()
        if(intent.hasExtra("waitAddData")){
            var addData = intent.getSerializableExtra("waitAddData")
            addData?.apply {
                waitAddData = this as EditClueParam
            }
        }
        var keliudata: Data? = null
        if(intent.hasExtra("keliudata")) {
            keliudata = (intent.getSerializableExtra("keliudata") as Data)
        }
        var levelLock:Boolean = false
        if(intent.hasExtra("levelLock")){
            levelLock = intent.getBooleanExtra("levelLock",false)
        }
        var canceldrive:Int = 0
        if(intent.hasExtra("canceldrive")){
            canceldrive = intent.getIntExtra("canceldrive",0)
        }

        var factory = InjectorUtils.getClueFollowUpActivityViewModelFactory(data, leve,come,keliudata,isNew,waitAddData,levelLock,canceldrive)
        return ViewModelProviders.of(this, factory).get(ClueFollowUpActivityViewModel::class.java)
    }

    private var listFragmet = ArrayList<Fragment>()
    override fun initView() {
        //基础跟进
        listFragmet.add(ClueFollowUpFragment())
        //订车
        listFragmet.add(BookingCarFragment())
        //提车
        listFragmet.add(LiftCarFragment())
        //等待库存
        listFragmet.add(WaitStockFragment())
        //订车提车
        listFragmet.add(BookingLiftCarFragment())
        val pagerAdapter = HomePageAdapter(supportFragmentManager, listFragmet)
        binding.viewPager.adapter = pagerAdapter
        binding.viewPager.offscreenPageLimit = 5

        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                binding.viewModel?.viewPagerPos = position
            }
        })
    }

    override fun initData() {
        binding.viewModel?.PagerPos?.observe(this, Observer {
            binding.viewPager.currentItem = it
        })
    }

    override fun onRequestPermissionsResult(requestCode: Int, @NonNull permissions: Array<String>, @NonNull grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(data!=null)
            super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            //订车车型
            102 -> {
                when (requestCode) {
                    101 -> {
                        //订车
                        data?.apply {
                            var data = data?.getSerializableExtra("car") as CarBrandParam
                            binding.viewModel?.setCardata(data)
                        }
                    }
                    102 -> {
                        //提车
                        data?.apply {
                            var data = data?.getSerializableExtra("car") as CarBrandParam
                            binding.viewModel?.setCardataLiftcar(data)
                        }
                    }
                    103 -> {
                        //提车,订车一体
                        data?.apply {
                            var data = data?.getSerializableExtra("car") as CarBrandParam
                            binding.viewModel?.setCardataBookingLiftcar(data)
                        }
                    }
                    104 -> {
                        //跟进
                        data?.apply {
                            var data = data?.getSerializableExtra("car") as CarBrandParam
                            binding.viewModel?.setFollowCardata(data)
                        }
                    }
                }
            }
            //订车到店
            101 -> {
                data?.apply {
                    var data = data?.getSerializableExtra("data") as Data
                    binding.viewModel?.setDaoDaian(data)
                }
            }
            //预订单
            104 -> {
                data?.apply {
                    var data = data?.getSerializableExtra("data") as OrderChooseData
                    binding.viewModel?.setOrder(data)
                }
            }
            //地区选择
            103 -> {
                //订车 省市区
                data?.apply {
                    var shen = getSerializableExtra("shen") as CarPinPaParam
                    var shi = getSerializableExtra("shi") as CarPinPaParam
                    var que = getSerializableExtra("que") as CarPinPaParam
                    var show = shen.title + " " + shi.title + " " + que.title
                    var id = shen.id.toString() + "," + shi.id.toString() + "," + que.id.toString()
                    binding.viewModel?.setCityData(show, id)

                }
            }
        }
    }

}
